package com.meichis.yndms.vm;

import androidx.lifecycle.MutableLiveData;

import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.yndms.model.ClientInfo;
import com.meichis.yndms.model.ShopInfo;
import com.meichis.yndms.model.VisitTemplate;
import com.meichis.yndms.model.VisitWork;
import com.meichis.yndms.repository.manager.SyncBaseDataManager;
import com.meichis.yndms.room.AppDatabase;
import com.meichis.yndms.room.DatabaseCallback;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-05-08 15:26
 */
public class VstTemplateVM extends MyViewModel {
    private ShopInfo ShopInfo;
    private ClientInfo clientInfo;
    private MutableLiveData<VisitWork> work;
    private MutableLiveData<Boolean> isSyncVstTemplate;
    private MutableLiveData<VisitTemplate> template;
    private DatabaseCallback<VisitWork> visitWorkCallback;
    public int syncCount = 0;


    public ClientInfo getClientInfo() {
        return clientInfo;
    }

    public void setClientInfo(ClientInfo clientInfo) {
        this.clientInfo = clientInfo;
    }

    public ShopInfo getShopInfo() {
        return ShopInfo;
    }

    public void setShopInfo(ShopInfo shopInfo) {
        ShopInfo = shopInfo;
    }

    public MutableLiveData<VisitWork> getWork() {
        if (work == null){
            work = new MutableLiveData<>();
        }
        return work;
    }

    public MutableLiveData<Boolean> getIsSyncVstTemplate() {
        if (isSyncVstTemplate == null) {
            isSyncVstTemplate = new MutableLiveData<>();
        }
        return isSyncVstTemplate;
    }

    public void getVisitWork(int client) {
        visitWorkCallback = new DatabaseCallback<VisitWork>() {
            @Override
            public void onSuccess(VisitWork work) {
               getWork().setValue(work);
            }
        };
        AppDatabase.getInstance().visitWorkDao().find(client, DateUtil.getCurDateStr(DateUtil.FORMAT_YMD))
                .subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(visitWorkCallback);
    }


    public MutableLiveData<VisitTemplate> getTemplate() {
        if (template == null) {
            template = new MutableLiveData<>();
        }
        if (template.getValue() == null){
            template.setValue(AppDatabase.getInstance().visitTemplateDao().findByCode("VT100_YNLBAPP"));
        }
        return template;
    }


    public void GetVisitTemplateList() {
        SyncBaseDataManager.getInstance().GetVisitTemplateList(getShowDialog(), getIsSyncVstTemplate());
    }
}
